﻿using ShapeManager;
using System;
using System.Collections.Generic;

namespace Commands
{
    public class CommandDelete : ICommand
    {
        List<DrawObject> cloneList;    // 包含已删除的选定项目。
        List<int> indexList;           // 包含已选删除项目的索引。
        /// <summary> 在应用“删除”功能之前，先创建此命令。 </summary>
        public CommandDelete(ShapeList shapeList)
        {
            cloneList = new List<DrawObject>();
            indexList = new List<int>();
            int count = shapeList.Count;
            for (int i = count - 1; i >= 0; i--)
            {
                if (shapeList[i].IsSelected)
                {
                    cloneList.Add(shapeList[i].Clone());
                    indexList.Add(i);
                }
            }
        }
        /// <summary> 将 “cloneList” 中的所有对象添加到列表中。  </summary>
        public void Undo(ShapeList list)
        {
            list.UnselectAll();
            int count = cloneList.Count;
            for (int i = count - 1; i >= 0; i--)
                list.Insert(indexList[i], cloneList[i]);
        }
        /// <summary> 从列表中删除所有保存在“cloneList”中的对象 </summary>
        public void Redo(ShapeList list)
        {
            int count = indexList.Count;
            for (int i = count - 1; i >= 0; i--)
                list.RemoveAt(indexList[i]);
        }
    }
}
